package 力扣日常刷题.木22一月.第13天0121;

/**
 * @author 帅小伙
 * @date 2022/1/21
 * @description
 * 非常好理解的dp
 */
public class Demo41动态规划中等猜数字大小 {

    public int getMoneyAmount(int n) {
        int[][] dp = new int[n + 1][n + 1];
        return dfs(dp, 1, n);
    }

    int dfs(int[][]dp,int l,int r){
        if(l >= r) return 0;
        if(dp[l][r]!=0) return dp[l][r];
        int res = Integer.MAX_VALUE;
        for (int i = l; i <= r ; i++) {
            int cost = i + Math.max(dfs(dp,l,i-1),dfs(dp,i+1,r));
            res = Math.min(res,cost);
        }
        dp[l][r] = res;
        return res;
    }


    public int getMoneyAmount2(int n) {
        int[][] f = new int[n + 1][n + 1];
        for (int i = n - 1; i >= 1; i--) {
            for (int j = i + 1; j <= n; j++) {
                f[i][j] = j + f[i][j - 1];
                for (int k = i; k < j; k++) {
                    f[i][j] = Math.min(f[i][j], k + Math.max(f[i][k - 1], f[k + 1][j]));
                }
            }
        }
        return f[1][n];
    }




}
